package org.jeecg.modules.business.takestockplan.controller;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlanDifferenceMaterial;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlanExecute;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlanExecuteMaterial;
import org.jeecg.modules.business.takestockplan.entity.VO.*;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanExecuteService;
import org.jeecg.common.api.vo.Result;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 盘点执行
 * @Author: jeecg-boot
 * @Date:   2023-08-04
 * @Version: V1.0
 */
@Api(tags="盘点执行")
@RestController
@RequestMapping("/business/takeStockPlanExecute")
@Slf4j
public class TakeStockPlanExecuteController {
	@Autowired
	private ITakeStockPlanExecuteService takeStockPlanExecuteService;

	/**
	 * 分页列表查询
	 *
	 * @param takeStockPlanExecute
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="盘点执行-分页列表查询", notes="盘点执行-分页列表查询")
	@GetMapping(value = "/list")
	public Result<Page<TakeStockPlanExecuteVO>> queryPageList(TakeStockPlanExecuteVO takeStockPlanExecute,
															  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															  HttpServletRequest req) {
		Result<Page<TakeStockPlanExecuteVO>> result = new Result<>();
		Page<TakeStockPlanExecuteVO> pageList = new Page<>(pageNo,pageSize);
		pageList = takeStockPlanExecuteService.listByPage(pageList, takeStockPlanExecute);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	 *   添加
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "盘点执行-添加")
	@ApiOperation(value="盘点执行-添加", notes="盘点执行-添加")
    @RequiresPermissions("business:takeStockPlanExecute:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TakeStockPlanExecuteAddVO vo) {
		takeStockPlanExecuteService.saveExecute(vo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param vo
	 * @return
	 */
	@AutoLog(value = "盘点执行-编辑")
	@ApiOperation(value="盘点执行-编辑", notes="盘点执行-编辑")
    @RequiresPermissions("business:takeStockPlanExecute:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TakeStockPlanExecuteAddVO vo) {
		takeStockPlanExecuteService.updateExecute(vo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "盘点执行-通过id删除")
	@ApiOperation(value="盘点执行-通过id删除", notes="盘点执行-通过id删除")
    @RequiresPermissions("business:takeStockPlanExecute:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		takeStockPlanExecuteService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="盘点执行-通过id查询", notes="盘点执行-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TakeStockPlanExecuteMaterialVO> queryById(@RequestParam(name="id",required=true) String id) {
		return Result.OK(takeStockPlanExecuteService.getExecuteById(id));

	}
	
	 /**
	  * 获取新增时详情
	  *
	  * @param wmsTakeStockPlanId
	  * @return
	  */
	 @ApiOperation(value="盘点执行-获取新增时详情", notes="盘点执行-获取新增时详情")
	 @GetMapping(value = "/getAddDetail")
	 public Result<TakeStockPlanExecuteMaterialVO> getAddDetail(@RequestParam(name="wmsTakeStockPlanId",required=true) String wmsTakeStockPlanId) {
		 TakeStockPlanExecuteMaterialVO vo = takeStockPlanExecuteService.getAddDetail(wmsTakeStockPlanId);
		 return Result.OK(vo);
	 }

	 /**
	  * 生成盘盈盘亏单
	  *
	  * @return
	  */
	 @ApiOperation(value="盘点执行-生成盘盈盘亏单", notes="盘点执行-生成盘盈盘亏单")
	 @PostMapping(value = "/addTakeStockResult")
	 @RequiresPermissions("business:takeStockPlanExecute:addTakeStockResult")
	 public Result<String> addTakeStockResult(@RequestParam(name="id",required=true) String id) {
		 takeStockPlanExecuteService.addTakeStockResult(id);
		 return Result.OK("操作成功！");
	 }

	 /**
	  * 复盘
	  *
	  * @return
	  */
	 @ApiOperation(value="盘点执行-复盘", notes="盘点执行-复盘")
	 @PostMapping(value = "/replay")
	 @RequiresPermissions("business:takeStockPlanExecute:edit")
	 public Result<String> replay(@RequestBody TakeStockPlanExecuteReplayVO vo) {
		 return Result.OK(takeStockPlanExecuteService.replay(vo.getId(), vo.getResult()));
	 }

	/**
	 * 盘点分析分页
	 *
	 * @param takeStockPlanExecute
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="盘点分析分页", notes="盘点分析分页")
	@GetMapping(value = "/pageAnalysis")
	public Result<Page<TakeStockPlanExecuteVO>> pageAnalysis(TakeStockPlanExecuteVO takeStockPlanExecute,
															  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		Result<Page<TakeStockPlanExecuteVO>> result = new Result<>();
		Page<TakeStockPlanExecuteVO> pageList = new Page<>(pageNo,pageSize);
		pageList = takeStockPlanExecuteService.pageAnalysis(pageList, takeStockPlanExecute);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 * 盘点对比
	 *
	 * @return
	 */
	@ApiOperation(value="盘点执行-盘点对比", notes="盘点执行-盘点对比")
	@GetMapping(value = "/contrast")
	public Result<TakeStockPlanExecuteMaterialVO> contrast(String takeStockPlanId) {
		return Result.OK(takeStockPlanExecuteService.contrast(takeStockPlanId));
	}

	/**
	 * 盘点对比保存备注
	 *
	 * @return
	 */
	@ApiOperation(value="盘点执行-盘点对比保存备注", notes="盘点执行-盘点对比保存备注")
	@PostMapping(value = "/saveContrast")
	public Result<String> saveContrast(@RequestBody List<TakeStockPlanExecuteMaterial> list) {
		takeStockPlanExecuteService.saveContrast(list);
		return Result.OK("保存成功！");
	}

	/**
	 * 盘点差异处理
	 *
	 * @return
	 */
	@ApiOperation(value="盘点执行-盘点差异处理", notes="盘点执行-盘点差异处理")
	@GetMapping(value = "/difference")
	public Result<TakeStockPlanExecuteMaterialVO> difference(String takeStockPlanId) {
		return Result.OK(takeStockPlanExecuteService.difference(takeStockPlanId));
	}

	/**
	 * 盘点作业导出excel
	 *
	 */
	@PostMapping(value = "/export")
	@ApiOperation(value="盘点作业-导出excel", notes="盘点作业-导出excel")
	public ModelAndView export(@RequestBody TakeStockPlanExecute execute) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<TakeStockPlanExecuteExport> exports = new ArrayList<>();
		TakeStockPlanExecuteMaterialVO byId = takeStockPlanExecuteService.getExecuteById(execute.getId());
		TakeStockPlanExecuteExport export = BeanUtil.copyProperties(byId, TakeStockPlanExecuteExport.class);
		List<TakeStockPlanExecuteMaterialExport> list = new ArrayList<>();
		for (TakeStockPlanExecuteMaterial material : byId.getTakeStockPlanExecuteMaterialList()) {
			TakeStockPlanExecuteMaterialExport materialExport = BeanUtil.copyProperties(material, TakeStockPlanExecuteMaterialExport.class);
			list.add(materialExport);
		}
		export.setList(list);
		exports.add(export);

		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "盘点作业");
		mv.addObject(NormalExcelConstants.CLASS, TakeStockPlanExecuteExport.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("盘点作业数据", "导出人:"+sysUser.getRealname(), "盘点作业"));
		mv.addObject(NormalExcelConstants.DATA_LIST, exports);
		return mv;
	}

	/**
	 * 盘点对比导出excel
	 *
	 * @param takeStockPlanExecute
	 */
	@ApiOperation(value="盘点执行-盘点对比导出excel", notes="盘点执行-盘点对比导出excel")
	@PostMapping(value = "/exportXls")
	public ModelAndView exportXls(@RequestBody TakeStockPlanExecute takeStockPlanExecute) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		TakeStockPlanExecuteMaterialVO contrast = takeStockPlanExecuteService.contrast(takeStockPlanExecute.getWmsTakeStockPlanId());

		// Step.3 组装pageList
		List<TakeStockPlanContrastExport> pageList = new ArrayList<TakeStockPlanContrastExport>();
		TakeStockPlanContrastExport vo = new TakeStockPlanContrastExport();
		BeanUtils.copyProperties(contrast, vo);
		List<TakeStockPlanExecuteMaterial> list = contrast.getTakeStockPlanExecuteMaterialList();
		ArrayList<TakeStockPlanContrastMaterialExport> exports = new ArrayList<>();
		for (TakeStockPlanExecuteMaterial material : list) {
			TakeStockPlanContrastMaterialExport export = BeanUtil.copyProperties(material, TakeStockPlanContrastMaterialExport.class);
			exports.add(export);
		}
		vo.setList(exports);
		pageList.add(vo);

		// Step.4 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "盘点对比");
		mv.addObject(NormalExcelConstants.CLASS, TakeStockPlanContrastExport.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("盘点对比", "导出人:"+sysUser.getRealname(), "盘点作业"));
		mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		return mv;
	}

	/**
	 * 根据物料查询账存数量
	 *
	 * @return
	 */
	@ApiOperation(value="盘点执行-根据物料查询账存数量", notes="盘点执行-根据物料查询账存数量")
	@GetMapping(value = "/getStockQuantity")
	public Result<BigDecimal> getStockQuantity(TakeStockPlanExecuteMaterial material) {
		return Result.OK(takeStockPlanExecuteService.getStockQuantity(material));
	}

	/**
	 * 盘点分析导出excel
	 *
	 */
	@PostMapping(value = "/exportAnalysis")
	@ApiOperation(value="盘点分析-导出excel", notes="盘点分析-导出excel")
	public ModelAndView exportAnalysis(@RequestBody TakeStockPlanExecuteVO takeStockPlanExecute) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<TakeStockPlanExecuteAnalysisExport> exports = new ArrayList<>();
		Page<TakeStockPlanExecuteVO> pageList = new Page<>(1,5);
		pageList = takeStockPlanExecuteService.pageAnalysis(pageList, takeStockPlanExecute);
		TakeStockPlanExecuteVO executeVO = pageList.getRecords().get(0);

		TakeStockPlanExecuteAnalysisExport export = BeanUtil.copyProperties(executeVO, TakeStockPlanExecuteAnalysisExport.class);
		TakeStockPlanExecuteMaterialVO byId = takeStockPlanExecuteService.getExecuteById(executeVO.getId());
		List<TakeStockPlanExecuteMaterial> materials = byId.getTakeStockPlanExecuteMaterialList();
		List<TakeStockPlanExecuteMaterialAnalysisExport> list = new ArrayList<>();
		for (TakeStockPlanExecuteMaterial material : materials) {
			if (!"1".equals(material.getResult()) && !"2".equals(material.getResult()) && !"3".equals(material.getResult())) {
				continue;
			}
			if ("1".equals(material.getResult())) {
				material.setResult("盘盈");
			}
			if ("2".equals(material.getResult())) {
				material.setResult("盘亏");
			}
			if ("3".equals(material.getResult())) {
				material.setResult("位置错误");
			}
			TakeStockPlanExecuteMaterialAnalysisExport copyProperties = BeanUtil.copyProperties(material, TakeStockPlanExecuteMaterialAnalysisExport.class);
			list.add(copyProperties);
		}
		export.setList(list);
		exports.add(export);
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "盘点分析");
		mv.addObject(NormalExcelConstants.CLASS, TakeStockPlanExecuteAnalysisExport.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("盘点分析数据", "导出人:"+sysUser.getRealname(), "盘点分析"));
		mv.addObject(NormalExcelConstants.DATA_LIST, exports);
		return mv;
	}

	/**
	 * 通过excel导入物料明细
	 *
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@ApiOperation(value="盘点作业-通过excel导入物料明细", notes="盘点作业-通过excel导入物料明细")
	public Result<List<TakeStockPlanDifferenceMaterial>> importExcel(@RequestPart("file") MultipartFile file, String id) {
		return takeStockPlanExecuteService.importExcel(file, id);
	}
}
